﻿package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.geom.Matrix;
	import vo.ColorChangeVO;
	
	/**
	 * ...
	 * @author Daniel Sheldon
	 */
	public class PheromoneTrails extends Bitmap
	{	
		private var timer:Timer = new Timer(33);
		private var _width:Number;
		private var _height:Number;
		private var changes:Dictionary = new Dictionary();
		
		public function PheromoneTrails(width, height) 
		{
			super();
			
			_width = width;
			_height = height;
			
			addEventListener(Event.ADDED_TO_STAGE, init)
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			bitmapData = new BitmapData(_width, _height, true, 0x00000000);
						
			Ant.timer.addEventListener(TimerEvent.TIMER, update);
			
			for (var i:int = 0; i < 10; i++)
			{
				for (var j:int = 0; j < 10; j++)
				{
					bitmapData.setPixel32(i + 10, j + 10, Ant.FOOD);
				}
			}
			//timer.start();
			
		}
		
		public function addChange(x:int,y:int,color:uint):void
		{
			var key:String = x + "-" + y;
			//var obj:ColorChangeVO = changes[key];
			
			/*if (obj != null)
			{
				obj.color = (obj.color < color) ? color : obj.color;
			}else{*/
				changes[key] = new ColorChangeVO(x,y,color);
			//}
			
		}
		
		private function update(e:TimerEvent):void 
		{
			bitmapData.lock();
			
			for each (var value:ColorChangeVO in changes) 
			{
				bitmapData.setPixel32(value.x, value.y, value.color);
			}
			
			changes = new Dictionary();
			
			
			//bitmapData.draw(bitmapData);
			//bitmapData.colorTransform(getRect(stage), new ColorTransform(1, 1, 1, 1, -1, -1, -1, -1));
			//bitmapData.colorTransform(getRect(stage), new ColorTransform(1, 1, 1, 1, 0, 0, 0, -1));
			
			//bitmapData.colorTransform(getRect(stage), new ColorTransform(1, 1, 1, .999, 0, 0, 0, 0));
			
			bitmapData.unlock();
		}
	}
}